<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends CI_Controller {
	
	var $data;	
	var $settings;
	
	public function __construct()
	{
		parent::__construct();
		
		//$nav = $this->data_fetcher->fetch_table('su-tb_sys_admin_nav');
		$settings = $this->data_fetcher->fetch_table('su-tb_sys_settings');
		foreach($settings as $setting)
		{
			$this->settings[$setting['strSettingName']] = $setting['strSettingValue'];
		}
		
		$this->config->load('admin');
		$this->load->model('cms_users');	
		$this->picture_fields = $this->config->item('picture_fields');				
		$this->foreign_fields = $this->config->item('foreign_fields');
		$this->data['site_name'] = $this->settings['site-name'];		
		if($this->logged_in())
			$this->data['nav'] = $this->get_admin_nav($this->session->userdata('strUserLevel'));
		
		
	}
	
	public function index()
	{
		if($this->logged_in())
		{
			$this->load->view('/header',$this->data);					
			$this->load->view('/dashboard');
			$this->load->view('/footer');
			
		}
		else
			redirect(base_url().'admin/login_view');
	}
	
	private function get_admin_nav($user_level)
	{
		$nav = '';
		$this->config->load('navigation');
		$nav_array = $this->config->item('nav_'.$user_level);
		foreach($nav_array as $key=>$value){			
			$nav.= '<div class="nav-collapse collapse navbar-responsive-collapse">';
			$nav.= '<ul class="nav">';                      
			$nav.= '<li class="dropdown';
			
			if(count($value) <= 1)
				if($value['table']==$this->uri->segment(3))	
					$nav .= ' active';				
				else
					$nav .= '';											
			else
				if(in_array($this->uri->segment(3),$value['table']))	
					$nav .= ' active';				
				else
					$nav .= '';	
					
			
			$nav.= '">';
			$nav.= '<a href="#" class="dropdown-toggle" data-toggle="dropdown">'.$key.' <b class="caret"></b></a>';
			$nav.= '<ul class="dropdown-menu">';			
			if(count($value) <= 1){				
				$nav.= '	<li><a href="'.base_url().'admin/view_list/'.$value['table'].'">View List</a></li>';
				$nav.= '	<li><a href="'.base_url().'admin/view_form/'.$value['table'].'">Add New</a></li>';
			}
			else
			{
				
				foreach($value as $key2=>$val2){
					if($key2!= 'table'){	
						$nav.= "<li class='dropdown-submenu'>
							<a href='#'>".$key2."</a>";
						$nav.= '<ul class="dropdown-menu">';
						if(isset($val2['strAddLink']) && isset($val2['strViewLink'])){							
							$nav.= '	<li><a href="'.base_url().'admin/'.$val2['strViewLink'].'/'.$val2['table'].'">View List</a></li>';
							$nav.= '	<li><a href="'.base_url().'admin/'.$val2['strAddLink'].'/'.$val2['table'].'">Add New</a></li>';
						}
						else
						{
							$nav.= '	<li><a href="'.base_url().'admin/view_list/'.$val2['table'].'">View List</a></li>';
							$nav.= '	<li><a href="'.base_url().'admin/view_form/'.$val2['table'].'">Add New</a></li>';
						}
						
						$nav.="</ul>";		
						$nav.= '</li>';
					}					
				}
				
			}
			$nav.= '</ul>';
			$nav.= '</li>';
			$nav.= '</ul>';                                        
			$nav.= '</div>';		
		}
		
		return $nav;
	
	}
		
	public function view_list($table=null)
	{
		if($this->logged_in())
		{
			if($this->is_super_function($table))
			{			
				$this->config->load('relational_db');
				$selected_fields = $this->config->item($table);								
				if($selected_fields!="")
					$empty = true;
				else
				{
					$empty = false;
					$selected_fields = array();
				}
					
				$fields = $this->db->field_data($table);
				$field_count = 1;
				$items = $this->data_fetcher->fetch_table($table);
				if(!empty($items)){
					$view = '<table class="table table-bordered table-striped" id="dt-view" style="font-size:11px"><thead><tr>';
					foreach($fields as $field)
					{
						if(!$empty || in_array ($field->name,$selected_fields)){
							$temp = explode('-',$field->name);
							if($field->name!='intID' && $field->name!='strPass' && $temp[0]!='ct'){								
								$view.='<th>'.get_label($table,$field->name).'</th>';
								$field_count++;
							}
						}
					}
					$view.='<th>Actions</th>';
					$view.='</tr></thead><tbody>';
					foreach($items as $item)
					{
						$view.='<tr>';
						foreach($fields as $field)
						{
							if(!$empty || in_array ($field->name,$selected_fields)){
								$temp = explode('-',$field->name);
								if($field->name!='intID' && $field->name!='strPass' && $temp[0]!='ct')
								{
									$temp = explode('-',$field->name);
									if($temp[0] == 'fk')
										$view.='<td>'.get_name_from_id($item[$field->name],$temp[1]).'</td>';									
									else
										$view.='<td>'.$item[$field->name].'</td>';
								}
							}
						}
						
						$link = base_url().'admin/view_form/'.$table.'/'.$item['intID'];
						$delete ='<a href="#delete-item" role="button" rel="'.$table.'" id="id-'.$item['intID'].'" class="delete-modal btn btn-small btn-danger" data-toggle="modal">Delete</a>';
						$view.='<td><a class="btn btn-small" href='.$link.'>Edit</a> ';
						$view.=$delete.'</td>';
						$view.='</tr>';
					}
					
					$view.='</tbody></table>';
				}
				else
				{
					$view = '<h3>No Data to display</h3>';
				}
						
				$this->data['table_view'] = $view;
				$this->data['table'] = $table;		
				$this->data['field_count'] = $field_count;	
				$this->load->view('/header',$this->data);		
				$this->load->view('/list_standard',$this->data);
				$this->load->view('/footer');
				$this->load->view('/list_standard_js',$this->data);
			}
			else
			{
				$this->load->view('/header',$this->data);
				$this->load->view('/restricted',$this->data);
				$this->load->view('/footer',$this->data);
			}
		}
		else
			redirect(base_url().'admin/login_view');
	}
	
	public function delete_item()
	{
		$post = $this->input->post();
		$temp = explode('-',$post['id']);
		$id = $temp[1];
		$data['message'] = $id;
		$this->data_poster->delete_data($post['table'],$id);
		echo json_encode($data);
		
	}
	public function attach_file()
	{
		if($this->logged_in())
		{
			$post = $this->input->post();			
			$data[$post['fieldname']] = $post['filename'];			
			$data['intID'] = $post['id'];			
			$this->data_poster->post_data($post['table'],$data,$post['id']);			
			
			echo json_encode($post);
		}
		else
		{
			$return['message'] = 'error';
			echo json_encode($return);
		}
	}
	
	public function detach_file_gallery()
	{
		if($this->logged_in())
		{
			$post = $this->input->post();
			
			unset($data);			
			$data['fk-tb_mas_content'] = 0;						
			$this->data_poster->update_image($data,$post['id'],'intID');
						
			echo json_encode($post);
		}
		else
		{
			$return['message'] = 'error';
			echo json_encode($return);
		}
	}
	
	public function attach_file_gallery()
	{
		if($this->logged_in())
		{
			$post = $this->input->post();
							
			$data['fk-tb_mas_content'] = $post['id'];						
			$this->data_poster->update_image($data,$post['filename']);
						
			echo json_encode($post);
		}
		else
		{
			$return['message'] = 'error';
			echo json_encode($return);
		}
	}
	
	public function file_upload($table=null,$id=null,$field_name=null)
	{	
		if($this->logged_in())
		{
			$data['table'] = $table;
			$data['id'] = $id;
			$data['field_name'] = $field_name;
			$this->load->view('/file_uploader_head',$data);
			$this->load->view('/file_uploader',$data);
		}
		else
		{
			echo "system error";
		}
	
	}
	
	public function file_upload_gallery($table=null,$id=null)
	{	
		if($this->logged_in())
		{
			$data['table'] = $table;
			$data['id'] = $id;
			$this->load->view('/file_uploader_head',$data);
			$this->load->view('/file_uploader_gallery',$data);
		}
		else
		{
			echo "system error";
		}
	
	}
	
	public function edit_profile()
	{
		if($this->logged_in())
		{
			$this->view_form('su-tb_sys_users',$this->session->userdata('intID'),null,1);
		}
		else
		{
			redirect(base_url().'admin/login_view');
		}
	}
	
	public function view_content_list($type)
	{
		if($this->logged_in())
		{			
			$table="tb_mas_content";
			$this->config->load('relational_db');
			$selected_fields = $this->config->item($table);								
			if($selected_fields!="")
				$empty = true;
			else
			{
				$empty = false;
				$selected_fields = array();
			}
				
			$fields = $this->db->field_data($table);
			$field_count = 1;
			//get gallery content type id
			$gallery_ct = $this->db->get_where('tb_mas_content_types',array('strName'=>$type))->first_row();
			$items = $this->data_fetcher->fetch_table($table,null,null,array('fk-tb_mas_content_types'=>$gallery_ct->intID));
			
			if(!empty($items)){
				$view = '<table class="table table-bordered table-striped" id="dt-view" style="font-size:11px"><thead><tr>';
				foreach($fields as $field)
				{
					if(!$empty || in_array ($field->name,$selected_fields)){
						$temp = explode('-',$field->name);
						if($field->name!='intID' && $field->name!='strPass' && $temp[0]!='ct'){
							$view.='<th>'.get_label($table,$field->name).'</th>';
							$field_count++;
						}
					}
				}
				$view.='<th>Actions</th>';
				$view.='</tr></thead><tbody>';
				foreach($items as $item)
				{
					$view.='<tr>';
					foreach($fields as $field)
					{
						if(!$empty || in_array ($field->name,$selected_fields)){
							$temp = explode('-',$field->name);
							if($field->name!='intID' && $field->name!='strPass' && $temp[0]!='ct')
								$temp = explode('-',$field->name);
									if($temp[0] == 'fk')
										$view.='<td>'.get_name_from_id($item[$field->name],$temp[1]).'</td>';									
									else
										$view.='<td>'.$item[$field->name].'</td>';
						}
					}
					$link = base_url().'admin/view_'.$type.'_form/'.$table.'/'.$item['intID'];
					$delete ='<a href="#delete-item" role="button" rel="'.$table.'" id="id-'.$item['intID'].'" class="delete-modal btn btn-small btn-danger" data-toggle="modal">Delete</a>';
					$view.='<td><a class="btn btn-small" href='.$link.'>Edit</a> ';
					$view.=$delete.'</td>';
					$view.='</tr>';
				}
				
				$view.='</tbody></table>';
			}
			else
			{
				$view = '<h3>No Data to display</h3>';
			}
					
			$this->data['table_view'] = $view;
			$this->data['table'] = $table;		
			$this->data['field_count'] = $field_count;	
			$this->load->view('/header',$this->data);		
			$this->load->view('/list_standard',$this->data);
			$this->load->view('/footer');
			$this->load->view('/list_standard_js',$this->data);
		}
		else
			redirect(base_url().'admin/login_view');
	}
	
	public function view_show_form($table ='tb_mas_content',$id=null)
	{
		if($this->logged_in())
		{
			$form = '';											
			$fields = $this->db->field_data($table);
			$single = null;								
						
			if($id!=null)
			{
				$single = $this->data_fetcher->fetch_single_data($table,$id);
				$this->data['single'] = $single;
			}
			
			$form .= $this->generate_standard_form($fields,$table,$id,$single,'show');
									
			
			$this->data['form'] = $form;
			$this->data['table'] = $table;
			
			$this->load->view('/header',$this->data);
			$this->load->view('/form_standard',$this->data);		
			$this->load->view('/footer');
			$this->load->view('/form_standard_js',$this->data);
			
										
		}
		else
		{
			redirect(base_url().'admin/login_view');
		}
	}
	
	public function view_gallery_form($table ='tb_mas_content',$id=null)
	{
		if($this->logged_in())
		{
			$form = '';											
			$fields = $this->db->field_data($table);
			$single = null;								
					
			if($id!=null)
			{
				$single = $this->data_fetcher->fetch_single_data($table,$id);
				$this->data['single'] = $single;
			}		
							
			$form .= $this->generate_standard_form($fields,$table,$id,$single,'gallery');
			
			$gallery_images = $this->data_fetcher->fetch_table('tb_sys_media',null,null,array('fk-tb_mas_content'=>$single['intID']));
			
			$this->load->view('/header',$this->data);						
			$form.= "<hr class='clear' />";
			$form.= "<ul class='thumbnails'>";
			if($id != null)
			{
				if(!empty($gallery_images))
					foreach($gallery_images as $image)
					{
						$form.= "<li class='g-image-container span2'>
									<a href='#' data-toggle='tooltip' title='click to remove from gallery' class='thumbnail gallery-remove' rel='".$image['intID']."'><img src='".base_url().THUMB_UPLOAD_DIR.$image['strFileName']."'></a>
								</li>";
					}
				else
					$form.= "<hr class='clear' />
							<div class='control-group pull-left span6'>
								<h4>Gallery Empty</h4>
							</div>";
				
				$form.= "</ul>";
				$form.= "<div class='clear'></div>";
				$this->data['id'] = $id;
				$form.="<div class='control-group pull-left span6'>
						<label class='control-label' for='pic button'>Add Picture</label><div class='controls'>";						
				$form.='<a class="various btn btn-primary" data-fancybox-type="iframe" href="'.base_url().'admin/file_upload_gallery/'.$table.'/'.$id.'">Upload/Select Image</a>';
				$form.="</div></div>";
			}
			
			$this->data['form'] = $form;
			$this->data['table'] = $table;
			
			
			$this->load->view('/form_standard',$this->data);		
			$this->load->view('/footer');
			$this->load->view('/form_gallery_js',$this->data);
			
										
		}
		else
		{
			redirect(base_url().'admin/login_view');
		}
	}
	public function view_form($table=null,$id=null,$page=null,$profile_edit=null)
	{		
		if($this->logged_in())
		{
			if($profile_edit!=null)
			{
				$form = '';					
				$single = $this->data_fetcher->fetch_single_data($table,$id);
				$this->data['single'] = $single;				
				$this->load->view('/header',$this->data);
				
				$form.=
				"<div class='control-group pull-left span6'>
					<label class='control-label' for='strUser'>Username</label>
					<div class='controls'>".$single['strUser']."</div>
				</div>";
				
				$form.=
				"<div class='control-group pull-left span6'>
					<label class='control-label' for='strUser'>User Level</label>
					<div class='controls'>".$single['strUserLevel']."</div>
				</div>";
				
				$opts = array(
				  'name'        => 'strPass',
				  'id'          => 'strPass',
				  'class'		=>'postvar'				  
				);
				$opts['value'] =  isset($single['strPass'])?pw_unhash($single['strPass']):'';
				
				$form.=
				"<div class='control-group pull-left span6'>
					<label class='control-label' for='strPass'>Password</label>
					<div class='controls'>".form_password($opts)."</div>	
				</div>";
				
				$this->data['form'] = $form;
				$this->data['table'] = $table;
				
				if($page!=null)
				{
					$this->load->view('/'.$page.'/form_standard',$this->data);		
					$this->load->view('/footer');
					$this->load->view('/'.$page.'/form_standard_js',$this->data);
				}
				else
				{
					$this->load->view('/form_standard',$this->data);		
					$this->load->view('/footer');
					$this->load->view('/form_standard_js',$this->data);
				}
			}
			else
			{
				if($this->is_super_function($table))
				{
					$fields = $this->db->field_data($table);				
					$form = '';		
					$single = null;								
					
					
					if($id!=null)
					{
						$single = $this->data_fetcher->fetch_single_data($table,$id);
						$this->data['single'] = $single;
					}
					
					$form = $this->generate_standard_form($fields,$table,$id,$single);
					
					$this->data['form'] = $form;
					$this->data['table'] = $table;
					$this->load->view('/header',$this->data);
					
					if($page!=null)
					{
						$this->load->view('/'.$page.'/form_standard',$this->data);		
						$this->load->view('/footer');
						$this->load->view('/'.$page.'/form_standard_js',$this->data);
					}
					else
					{
						$this->load->view('/form_standard',$this->data);		
						$this->load->view('/footer');
						$this->load->view('/form_standard_js',$this->data);
					}
					
				}
				else
				{
				
					$this->load->view('/header',$this->data);
					$this->load->view('/restricted',$this->data);
					$this->load->view('/footer',$this->data);
				}
			}
		}
		else
		{
			redirect(base_url().'admin/login_view');
		}
		
	}
	
	public function get_add_modal($table,$id =null)
	{
		if($this->logged_in())
		{
			if($this->is_super_function($table))
			{
				$fields = $this->db->field_data($table);				
				$form = '';		
				$single = null;								
				
				
				if($id!=null)
				{
					$single = $this->data_fetcher->fetch_single_data($table,$id);
					$this->data['single'] = $single;
				}
				
				$form = $this->generate_standard_form($fields,$table,$id,$single);
				
				$this->data['form'] = $form;
				$this->data['table'] = $table;
				
				$this->load->view('/header_without_nav',$this->data);				
				$this->load->view('/form_standard',$this->data);		
				$this->load->view('/footer');				
				$this->load->view('/form_iframe_js',$this->data);		
								
				
			}
			else
			{
				$this->load->view('/header',$this->data);
				$this->load->view('/restricted',$this->data);
				$this->load->view('/footer',$this->data);
			}
		}
		else
		{
		  echo "error you are not logged in";
		}		
				
	}
	
	public function generate_standard_form($fields,$table,$id=null,$single = null,$type=null)
	{		
		$form = '';
		$ctr = 1;
		$temp = $this->config->item('uneditable');
		if(isset($temp[$table]))
			$uneditable = $temp[$table];
		else
			$uneditable = array();
		
			foreach($fields as $field)
			{
				if(!in_array($field->name,$uneditable))
				{
					$temp = explode('-',$field->name);
					$table_array = array();				
					
					if($temp[0] == 'fk')
					{				
						
						$fk_fields = $this->db->field_data($temp[1]);
						$foreign = $this->data_fetcher->fetch_table($temp[1]);
						foreach($foreign as $fr)
						{
							foreach($this->foreign_fields as $f)
							{								
								if(isset($fr[$f]))
									$table_array[$fr[$fk_fields[0]->name]] = $fr[$f];
							}
						}
						
						if($type != null && $field->name == 'fk-tb_mas_content_types')
						{
							$form.= "<input type='hidden' name='".$field->name."' value='".get_type_id($type)."' class='postvar'>";
						}
						else
						{
							$form.= 
							"<div class='control-group pull-left span9'>
								<label class='control-label' for='".$field->name."'>".get_label($table,$field->name)."</label>
								<div class='controls'>";					
								$form.=	form_dropdown($field->name, $table_array, isset($single[$field->name])?$single[$field->name]:'','class=postvar');
							$form.=
								"
									
								<a class='btn various fancybox.iframe' href='".base_url()."admin/get_add_modal/".$temp[1]."'>Add New</a>
								</div>
							</div>
							
							";
						}
							
							
					}
					elseif(($field->type == 'varchar' || $field->type == 'text' || $field->type == 'datetime') && !in_array($field->name,$this->picture_fields) && $field->name != 'strTable'  )
					{
						$opts = array(
						  'name'        => $field->name,
						  'id'          => $field->name,
						  'class'		=>'postvar',				  
						  'maxlength'   => $field->max_length
						);
						if($field->name!='strPass')						
							$opts['value'] =  isset($single[$field->name])?$single[$field->name]:'';
						else
							$opts['value'] =  isset($single[$field->name])?pw_unhash($single[$field->name]):'';
						
						if($temp[0] == 'ct')
							$opts['class'] =  'postvar content';
						
						if($field->type == 'datetime'){
							$opts['class'] =  'postvar datepicker';
							$opts['value'] =  isset($single[$field->name])?$single[$field->name]:date('Y-m-d H:i:s');
						}
							
						if($field->type == 'varchar' || $field->type == 'datetime'){
							if($field->name=='strPass')
							{
								$form.=
								"<div class='control-group pull-left span5'>
									<label class='control-label' for='".$field->name."'>".get_label($table,$field->name)."</label>
									<div class='controls'>".form_password($opts)."</div>
								</div>";
							}							
							else
							{
								$form.=
								"<div class='control-group pull-left span5'>
									<label class='control-label' for='".$field->name."'>".get_label($table,$field->name)."</label>
									<div class='controls'>".form_input($opts)."</div>
								</div>";
							}
						}				
						else
						{
							$form.=
							"<div class='control-group pull-left span9'>
								<label class='control-label' for='".$field->name."'>".get_label($table,$field->name)."</label>
								<div class='controls'>".form_textarea($opts)."</div>
							</div>";
						
						}
						
					}				
					elseif(in_array($field->name,$this->picture_fields))
					{
						if(isset($id))	
						{
							$this->data['id'] = $id;
							$form.="<div class='control-group pull-left span5'>
									<label class='control-label' for='".$field->name."'>".get_label($table,$field->name)."</label><div class='controls'>";
							if(isset($single[$field->name]) && $single[$field->name]!='' )
							{
								$form.="<h6 class='well'><img src='".base_url().THUMB_UPLOAD_DIR.$single[$field->name]."'><br/>".$single[$field->name];
								$form.='<a href="#remove-image" role="button" class="delete-modal btn btn-small btn-danger" rel="'.$field->name.'" data-toggle="modal">Remove Image</a></h5>';
							}
							
							$form.='<a class="various btn btn-primary" data-fancybox-type="iframe" href="'.base_url().'admin/file_upload/'.$table.'/'.$id.'/'.$field->name.'">Upload/Select Image</a>';
							$form.="</div></div>";
						}
					
					}
					
					elseif($field->name == 'strTable')
					{							
						$tables = $this->db->list_tables();	
						$table_array = array();
						foreach($tables as $table_item)
						{
							$table_array[$table_item] = $table_item;
						}					
						$form.= 
						"<div class='control-group pull-left span5'>
							<label class='control-label' for='".$field->name."'>".get_label($table,$field->name)."</label>
							<div class='controls'>".form_dropdown($field->name, $table_array, isset($single[$field->name])?$single[$field->name]:'','class=postvar')."</div>
						</div>";
						
					}
					elseif($field->type == 'enum')
					{				
						$options = array();
						$enums = get_enum_values($table,$field->name);				
						foreach($enums as $enum)
						{
							$options[$enum] = $enum;
						}
						$form.= 
						"<div class='control-group pull-left span5'>
							<label class='control-label' for='".$field->name."'>".get_label($table,$field->name)."</label>
							<div class='controls'>".form_dropdown($field->name, $options, isset($single[$field->name])?$single[$field->name]:'','class=postvar')."</div>
						</div>";
					}
					elseif($field->type == 'int' && $field->name != 'intID') 
					{						
						$value = isset($single[$field->name])?$single[$field->name]:'';
						$max = 100;
						$form.=
						"<div class='control-group pull-left span5'>
							<label class='control-label' for='".$field->name."'>".get_label($table,$field->name)."</label>
							<div class='controls'><input type='number' class='postvar' name='".$field->name."' id='".$field->name."' min='1' max='".$max."' value='".$value."'></div>
						</div>";
					}
							
										
				}
			}
			return $form;
	}
	
	public function login()
	{		
		$post = $this->input->post();		
		$auth = $this->cms_users->auth_user($post['strCMSUserName'],$post['strCMSUserPassword']);
		
		switch($auth)
		{
			case 'userError':
			$data['message'] = "That username does not exist";
			break;
			
			case 'passwordError':
			$data['message'] = "Invalid Password";
			break;
			
			default:
			$this->cms_users->session_init($auth);
			$data['message'] = "Valid";
			break;			
		}
		
		echo json_encode($data);
	}
	
	public function login_view()
	{				
		$this->load->view('/login');						
	}
	
	public function logout()
	{		
		$userid = $this->session->userdata('intID');
		$this->cms_users->logout_user($userid);
		redirect(base_url()."admin");
	}
	
	
	public function update_table($table)
	{
		$post = $this->input->post();
		
		if(isset($post['strPass']) && $post['strPass']!='')
		{
			$post['strPass'] = pw_hash($post['strPass']);
		}
		else
		{
			unset($post['strPass']);
		}
		
		$errors = $this->validate_table($table);
		if(!$errors)
		{
			//Last Edited Field for content logs
			if($table == 'tb_mas_content')
				$post['strLastEdited'] = $this->session->userdata('strUser');
				
			if(isset($post['intID']))
			{
				$this->data_poster->post_data($table,$post,$post['intID']);
				$data['redirect'] = '';				
			}
			else
			{
				$this->data_poster->post_data($table,$post);
				$this->session->set_flashdata('alert-message','Successfully Added New Entry');	
				$data['redirect'] = base_url().'admin/view_form/'.$table.'/'.$this->db->insert_id();
			}
			
			
				
			
			$data['message'] = "success";
		}
		else
		{
			$data['message'] = $errors;
		}
		
		
		echo json_encode($data);
		
	}
	
	public function validate_table($table)
	{
		$this->load->library('form_validation');
		$validation_array = $this->data_fetcher->get_validation_items($table);
		$config = array();
		foreach($validation_array as $validation){
			
			$config[] = array(
					'field'   => $validation['strField'], 
					'label'   => $validation['strLabel'], 
					'rules'   => $validation['strRules']
						);
		}
		
		$this->form_validation->set_rules($config);
		
		if ($this->form_validation->run() == FALSE)
		{
			$message=validation_errors();
		}
		else
		{
			$message = false;
		}
		return $message;
	}
	
	protected function logged_in()
	{
		if($this->session->userdata('admin_logged_in') && $this->session->userdata('strUserLevel') != 'member')
			return 1;
		else
			return 0;
	}
	
	protected function is_super_function($table)
	{
		$temp = explode('-',$table);
		if( ($temp[0] == 'su' && $this->session->userdata('strUserLevel') == 'super_user') || $temp[0] != 'su' )
		{
			$allowed_tables = $this->config->item($this->session->userdata('strUserLevel'));														
			if(in_array($table,$allowed_tables) || empty($allowed_tables))
				return 1;
			else
				return 0;						
		}
		else
		{
			return 0;
		}
	}
	
	
	
	
}